//
//  PhotoBrowserController.m
//  Browser
//
//  Created by ZHY on 2017/7/11.
//  Copyright © 2017年 ZHY. All rights reserved.
//


#import "PhotoBrowserController.h"
#import "PhotoBrowserCell.h"
#import "UIImageView+WebCache.h"

@interface PhotoBrowserController ()<UICollectionViewDelegate,UICollectionViewDataSource,UIScrollViewDelegate,PhotoBrowserCellDelegate>

@property (nonatomic,strong) UICollectionView *collectView;
/**
 *  显示页数
 */
@property (nonatomic,strong) UILabel *pageLabel;
/**
 *  保存按钮
 */
@property (nonatomic,strong) UIButton *saveBtn;
/**
 *  当前页面
 */
@property (nonatomic,assign) NSInteger currentPage;
/**
 *  已保存图片数组
 */
@property (nonatomic,strong) NSMutableArray *saveImageFlag;

@end

@implementation PhotoBrowserController


-(instancetype)init {

    if (self = [super init]) {
        self.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
   
    [self setupUI];
}





#pragma mark - setupUI 
-(void)setupUI {
    
    [self.view addSubview:self.collectView];
    [self.view addSubview:self.saveBtn];
    self.saveBtn.enabled = NO;
    self.currentPage = self.currentIndex;
    [self.collectView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:self.currentIndex inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
    
    if (self.imgs && self.imgs.count>1) {
        self.pageLabel.text = [NSString stringWithFormat:@"%ld / %ld",(long)self.currentIndex+1,(long)self.imgs.count];
    }else if(self.urls.count && self.urls.count>1) {
        self.pageLabel.text = [NSString stringWithFormat:@"%ld / %ld",(long)self.currentIndex+1,(long)self.urls.count];
    }
}

#pragma mark - private


#pragma mark - event


#pragma mark - collectView data source
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    if (self.imgs) {
        return self.imgs.count;
    }
    return self.urls.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    PhotoBrowserCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kPhotoBrowserCellID forIndexPath:indexPath];
    cell.httpHeaderFieldDic = self.httpHeaderFieldDic;
    cell.delegate = self;
    if (self.imgs) {
        cell.imgStr = self.imgs[indexPath.row];
    }else {
        cell.urlStr = self.urls[indexPath.row];
    }
    
    return cell;
}

#pragma mark - collectView delegate


#pragma mark - UIScrollViewDelegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    self.saveBtn.enabled = NO;
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    BOOL isShow;
    long count = 0;
    if (self.imgs && self.imgs.count>1) {
        isShow = YES;
        count = self.imgs.count;
    }else if(self.urls.count && self.urls.count>1) {
       isShow = YES;
        count = self.urls.count;
    }else {
        isShow = NO;
    }
    self.currentPage = scrollView.contentOffset.x/([UIScreen mainScreen].bounds.size.width+20);
    NSLog(@"self.currentPage:%ld",self.currentPage);
    if ([self.saveImageFlag containsObject:@(self.currentPage)]) {
        self.saveBtn.enabled = NO;
    }else {
        self.saveBtn.enabled = YES;
    }
    if (isShow) {
        self.pageLabel.text = [NSString stringWithFormat:@"%ld / %ld",(long)self.currentPage+1,(long)count];
    }
}

#pragma mark - custom delegate
-(void)settingSaveBtnStatus:(BOOL)isEnable {

    self.saveBtn.enabled = isEnable;
}

-(void)closePhotoBrowser {
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - private
-(void)saveBtnClick {
    
    PhotoBrowserCell *cell = (PhotoBrowserCell *)[self.collectView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:self.currentPage inSection:0]];
    [cell saveImage];
    [self.saveImageFlag addObject:@(self.currentPage)];
    
}


//隐藏状态栏
-(BOOL)prefersStatusBarHidden {
    return YES;
}

#pragma mark - set or get
-(UICollectionView *)collectView {
    
    if (!_collectView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.minimumLineSpacing = 0;
        layout.itemSize = CGSizeMake([UIScreen mainScreen].bounds.size.width+20, [UIScreen mainScreen].bounds.size.height);
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _collectView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
        _collectView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width+20, [UIScreen mainScreen].bounds.size.height);
        _collectView.delegate = self;
        _collectView.dataSource = self;
        _collectView.pagingEnabled = YES;
        _collectView.showsVerticalScrollIndicator = NO;
        _collectView.showsHorizontalScrollIndicator = NO;
        [_collectView registerClass:[PhotoBrowserCell class] forCellWithReuseIdentifier:kPhotoBrowserCellID];
        
    }
    return _collectView;
}

-(UILabel *)pageLabel {

    if (!_pageLabel) {
        _pageLabel = [[UILabel alloc] initWithFrame:CGRectMake(([UIScreen mainScreen].bounds.size.width-50)/2, [UIScreen mainScreen].bounds.size.height-40, 50, 20)];
        _pageLabel.textColor = [UIColor whiteColor];
        _pageLabel.backgroundColor = [UIColor clearColor];
        _pageLabel.userInteractionEnabled = NO;
        _pageLabel.font = [UIFont boldSystemFontOfSize:20];
        _pageLabel.textAlignment = NSTextAlignmentCenter;
        [self.view addSubview:_pageLabel];
    }
    return _pageLabel;
}

-(UIButton *)saveBtn {
    
    if (!_saveBtn) {
        _saveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _saveBtn.frame = CGRectMake(40, [UIScreen mainScreen].bounds.size.height-40, 24, 24);
        [_saveBtn setBackgroundImage:[UIImage imageNamed:@"save_icon"] forState:UIControlStateNormal];
        [_saveBtn setBackgroundImage:[UIImage imageNamed:@"save_icon_highlighted"] forState:UIControlStateHighlighted];
        [_saveBtn addTarget:self action:@selector(saveBtnClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _saveBtn;
}

-(NSMutableArray *)saveImageFlag {
    
    if (!_saveImageFlag) {
        _saveImageFlag = [[NSMutableArray alloc] init];
    }
    return _saveImageFlag;
}

@end
